/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.file;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * 一些解压缩工具
 *
 * @author Mr.css 2017-08-15 5:40:52
 */
public class ZipUtils {

    private ZipUtils() {
    }

    /**
     * 解压指定文件
     *
     * @param is      输入
     * @param charset 压缩包编码，一般是 GBK
     * @param path    指定的文件（路径 + 文件名）
     * @param os      输出
     * @throws IOException -
     */
    public static void copy(InputStream is, Charset charset, String path, OutputStream os) throws IOException {
        ZipEntry entry;
        try (ZipInputStream zis = new ZipInputStream(is, charset)) {
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.getName().equals(path)) {
                    IOUtils.copy(zis, os);
                    break;
                }
            }
            zis.closeEntry();
        }
    }

    /**
     * 将文件解压到指定文件
     *
     * @param charset 输入流编码（压缩文件是有编码的，360压缩用的是GBK）
     * @param is      输入流
     * @param root    目标目录
     * @throws IOException -
     */
    public static void unzip(InputStream is, Charset charset, File root) throws IOException {
        ZipEntry entry;
        try (ZipInputStream zis = new ZipInputStream(is, charset)) {
            while ((entry = zis.getNextEntry()) != null) {
                File file = new File(root, entry.getName());
                if (entry.isDirectory()) {
                    FileUtils.directory(file);
                } else {
                    FileUtils.file(file);
                    try (OutputStream os = Files.newOutputStream(file.toPath())) {
                        IOUtils.copy(zis, os);
                    }
                }
            }
            zis.closeEntry();
        }
    }
}
